package com.ygg.controller;

import com.ygg.model.cmn.Dict;
import com.ygg.result.Result;
import com.ygg.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api("数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Resource
    DictService dictService;

    @ApiOperation(value = "根据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result<List<Dict>> findChildData(@PathVariable Long id) {
        List<Dict> chlidData = dictService.findChlidData(id);
        return Result.ok(chlidData);
    }


    @ApiOperation(value = "导出数据字典")
    @GetMapping("exportDate")
    public void getDate(HttpServletResponse response) {
        try {
            dictService.exportDate(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "导入数据字典")
    @PostMapping("importDate")
    public Result<String> importDate(MultipartFile file) {
        dictService.importDate(file);
        return Result.ok();
    }


    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }


    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        return dictService.getName(dictCode, value);

    }

    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value) {
        return dictService.getName("", value);
    }


}
